<?php

namespace Beerus\Modules\NewProduct\Controllers;

use App\Admin\Extensions\Tools\GridView;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Biz\model\NewProduct\NewProductJoinRecordModel;
use Biz\model\NewProduct\NewProductTagModel;
use Biz\model\NewProduct\NewProductWikiModel;
use Biz\pockets\export\ExportData;
use Biz\service\NewProduct\NewProductService;
use Biz\service\wiki\AdmWiki;
use Doraemon\tools\BlockType;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\IdParse;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;


/**
 * Description: 参与记录
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/8/23
 * Time: 18:17
 */
class RecordController extends Controller
{
    public const  PAGE_SIZE = 20;

    /**
     * 获取列表
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function index(Request $request, Content $content): Content
    {
        $params = $request->all();
        $page_size = $params['per_page'] ?? self::PAGE_SIZE;
        $params['per_page'] = $page_size;

        $where = $this->_getWhere($params);
        $list = NewProductService::getRecordList($where);
        unset($where['LIMIT']);
        $count = NewProductJoinRecordModel::count($where);
        $gridModel = new GridDataModel($list, $count, [
            'perPage' => $page_size,
        ]);
        $grid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableCreateButton();
                // 去掉默认的id过滤器
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function ($filter) {
                    $filter->disableIdFilter();

                    // 在这里添加字段过滤器
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('nid', '活动ID')->placeholder('活动ID');
                        $filter->equal('type', '活动类型')->select([
                            '1' => '超级新品',
                            '2' => '甄选新品',
                        ]);
                        $filter->equal('brand_id', '品牌id')->placeholder('品牌id');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('start_time', '搜索开始时间')->datetime();
                        $filter->equal('end_time', '搜索结束时间')->datetime();
                    });
                    $filter->expand();//默认展开
                });
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('brand_id', '品牌id')->sortable();
                $grid->column('brand_name', '品牌名称');
                $grid->column('name', '商品名称');
                $grid->column('tag', '标签');
                $grid->column('uid', '用户id');
                $grid->column('fans', '粉丝');
                $grid->column('type', '活动类型')->using([
                    1 => '超级新品',
                    2 => '甄选新品',
                ]);

                $grid->column('create_time', '添加时间');
                $grid->disableActions();

            }
        );
        $content->row($grid);

        return $content
            ->header('新品速递-参与记录')
            ->description(' ')
            ->breadcrumb(['text' => '新品速递审核', 'url' => '/newProduct/index'], ['text' => '测评报告管理']);
    }

    /**
     * 获取查询条件
     * @param $params
     * @return array
     */
    private function _getWhere($params): array
    {
        $type = $params['type'] ?? 0;
        $page = $params['page'] ?? 1;
        $nid = $params['nid'] ?? 0;
        $page_size = $params['per_page'] ?? 20;
        $brand_id = $params['brand_id'] ?? 0;
        $start_time = $params['start_time'] ?: 0;
        $end_time = $params['end_time'] ?: 0;
        if ($nid) {
            $where['nid'] = $nid;
        }
        if ($type) {
            $where['type'] = $type;
        }
        if ($start_time) {
            $where['create_time[>=]'] = $start_time;
        }
        if ($end_time) {
            $where['create_time[<=]'] = $end_time;
        }
        $where['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size,
        ];
        if ($brand_id) {
            $where['brand_id'] = $brand_id;
        }
        return $where;
    }

    /**
     * 发送email
     * @param $list
     */
    private function _sendEmail($list)
    {
        foreach ($list as $item) {
            $nid = $item['nid'];
            $fans = $item['fans'] ?? 0;
            $type = $item['type'] == 1 ? '超级新品' : '甄选新品';
            $brand_name = $item['brand_name'] ?? '未知';
            $brand_id = $item['brand_id'] ?? '未知';
            $name = $item['name'] ?? '未知商品';
            $uid = $item['uid'] ?? 0;
            $create_time = $item['create_time'] ?? 0;
            $str .= "<tr><td>" . $brand_name . "</td><td>" . $brand_id . "</td><td>" . $name . "</td><td>"
                . $uid . "</td><td>" . $fans . "</td><td>" . $type . "</td><td>" . $create_time . "</td><td>" . $brand_name . "</td><td>" . $nid . "</td></tr>";
        }
        $tuo = 'tuoruiqing@haohaozhu.com';
        $cyk = 'congyukun@haohaozhu.com';
        $body = <<<HTML
            <style>
                table.gridtable {
                    font-family: verdana,arial,sans-serif;
                    font-size:11px;
                    color:#333333;
                    border-width: 1px;
                    border-color: #666666;
                    border-collapse: collapse;
                }
                table.gridtable th {
                    border-width: 1px;
                    padding: 8px;
                    border-style: solid;
                    border-color: #666666;
                    background-color: #dedede;
                }
                table.gridtable td {
                    border-width: 1px;
                    padding: 8px;
                    border-style: solid;
                    border-color: #666666;
                    background-color: #ffffff;
                }
            </style>
        <!-- Table goes in the document BODY -->
        <table class="gridtable">
        
        <tr>
            <th>品牌名称</th><th>品牌id</th><th>商品名称</th><th>用户id</th><th>粉丝</th><th>活动类型</th><th>添加时间</th><th>活动id</th>
        </tr>
            $str
        </table>
        HTML;
        \Biz\tools\Mail::send([$cyk, $tuo], "参与活动记录", $body);
    }


}